﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FrankJie.IDAL;
using FrankJie.Model;

namespace FrankJie.SQLServerDAL
{
	/// <summary>
	/// 管理员数据访问类
	/// </summary>
	public partial class Manager:IManager
	{
        /// <summary>
        /// 增加一个管理员
        /// </summary>
        /// <param name="model">管理员实体</param>
        /// <returns>返回管理员ID</returns>
		public int Add(ManagerInfo managerInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO tblManager(");
            strSql.Append("LoginName,PassWord,NickName,RoleId,Email,HomePage,CreateTime,LastLogTime,LastLogIp,Enabled)");
			strSql.Append(" VALUES (");
            strSql.Append("@LoginName,@PassWord,@NickName,@RoleId,@Email,@HomePage,@CreateTime,@LastLogTime,@LastLogIp,@Enabled)");
			strSql.Append(";SELECT @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.VarChar,50),
					new SqlParameter("@PassWord", SqlDbType.VarChar,50),
					new SqlParameter("@NickName", SqlDbType.VarChar,50),
                    new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@HomePage", SqlDbType.VarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLogTime", SqlDbType.DateTime),
					new SqlParameter("@LastLogIp", SqlDbType.VarChar,50),
					new SqlParameter("@Enabled", SqlDbType.Bit,1)};
			parameters[0].Value = managerInfo.LoginName;
			parameters[1].Value = managerInfo.PassWord;
			parameters[2].Value = managerInfo.NickName;
            parameters[3].Value = managerInfo.RoleId;
			parameters[4].Value = managerInfo.Email;
			parameters[5].Value = managerInfo.HomePage;
			parameters[6].Value = managerInfo.CreateTime;
			parameters[7].Value = managerInfo.LastLogTime;
			parameters[8].Value = managerInfo.LastLogIp;
			parameters[9].Value = managerInfo.Enabled;

			object obj = SQLHelper.GetSingle(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

        /// <summary>
        /// 更新一个管理员
        /// </summary>
        /// <param name="model">管理员实体</param>
        /// <returns>返回是否成功</returns>
        public bool Update(ManagerInfo managerInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE tblManager SET ");
            strSql.Append("NickName=@NickName,Email=@Email,HomePage=@HomePage");
			strSql.Append(" WHERE Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@NickName", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@HomePage", SqlDbType.VarChar,50)};
            parameters[0].Value = managerInfo.NickName;
            parameters[1].Value = managerInfo.Email;
            parameters[2].Value = managerInfo.HomePage;
            parameters[3].Value = managerInfo.Id;

			int rows=SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 更新最后登录信息
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="lastLogTime">最后登录时间</param>
        /// <param name="lastLogIp">最后登录IP</param>
        /// <returns>返回是否成功</returns>
        public bool UpdateLastLog(int id, DateTime lastLogTime, string lastLogIp)
        {
            string strSql = "UPDATE tblManager SET LastLogTime=@LastLogTime,LastLogIp=@LastLogIp WHERE Id=@Id";
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@LastLogTime", SqlDbType.DateTime),
                    new SqlParameter("@LastLogIp", SqlDbType.VarChar,50)};
            parameters[0].Value = id;
            parameters[1].Value = lastLogTime;
            parameters[1].Value = lastLogIp;

            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 管理员活动/禁用状态
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="flag">true为活动状态 false为禁用状态</param>
        /// <returns>返回是否成功</returns>
        public bool Enabled(int id, bool flag)
        {
            string strSql = "UPDATE tblManager SET Enabled=@Enabled WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@Enabled", SqlDbType.Bit)};
            parameters[0].Value = id;
            parameters[1].Value = flag;
            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个管理员
        /// </summary>
        /// <param name="Id">管理员ID</param>
        /// <returns>返回是否成功</returns>
		public bool Delete(int id)
		{
            string strSql = "DELETE FROM tblManager WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        /// <summary>
        /// 管理员登录名密码检查
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="passWord">密码</param>
        /// <param name="msg">返回消息</param>
        /// <returns>返回管理员实体</returns>
        public ManagerInfo Login(string loginName, string passWord, out string msg)
        {
            string strSql = "SELECT TOP 1 * FROM tblManager WHERE LoginName=@LoginName";
            SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.VarChar,50)};
            parameters[0].Value = loginName;
            ManagerInfo managerInfo = null;
            msg = string.Empty;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters))
            {
                if (!dr.HasRows)
                {
                    msg = "loginname";
                    return null;
                }
                managerInfo = new ManagerInfo();
                managerInfo.Id = int.Parse(dr["Id"].ToString());
                managerInfo.LoginName = dr["LoginName"].ToString();
                managerInfo.PassWord = dr["PassWord"].ToString();
                managerInfo.NickName = dr["NickName"].ToString();
                managerInfo.Email = dr["Email"].ToString();
                managerInfo.HomePage = dr["HomePage"].ToString();
                managerInfo.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                managerInfo.LastLogTime = DateTime.Parse(dr["LastLogTime"].ToString());
                if ((dr["Enabled"].ToString() == "1") || (dr["Enabled"].ToString().ToLower() == "true"))
                {
                    managerInfo.Enabled = true;
                }
                else
                {
                    managerInfo.Enabled = false;
                }
            }
            if (managerInfo.PassWord != passWord)
            {
                msg = "password";
                return null;
            }
            if (!managerInfo.Enabled)
            {
                msg = "unenabled";
                return null;
            }
            return managerInfo;
        }
	}
}

